package com.houweishi.module.fileShare.service.impl;

import com.houweishi.common.constant.Constants;
import com.houweishi.common.constant.HttpStatus;
import com.houweishi.common.core.domain.AjaxResult;
import com.houweishi.common.core.domain.model.LoginUser;
import com.houweishi.common.utils.DateUtils;
import com.houweishi.common.utils.SecurityUtils;
import com.houweishi.module.fileShare.domain.FileShare;
import com.houweishi.module.fileShare.mapper.FileShareMapper;
import com.houweishi.module.fileShare.service.IFileShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 文件分享Service业务层处理
 * 
 * @author houweishi
 * @date 2023-03-24
 */
@Service
public class FileShareServiceImpl implements IFileShareService 
{
    @Autowired
    private FileShareMapper fileShareMapper;

    /**
     * 查询文件分享
     * 
     * @param id 文件分享主键
     * @return 文件分享
     */
    @Override
    public FileShare selectFileShareById(Long id)
    {
        return fileShareMapper.selectFileShareById(id);
    }

    /**
     * 查询文件分享列表
     * 
     * @param fileShare 文件分享
     * @return 文件分享
     */
    @Override
    public List<FileShare> selectFileShareList(FileShare fileShare)
    {
        return fileShareMapper.selectFileShareList(fileShare);
    }

    /**
     * 新增文件分享
     * 
     * @param fileShareList 文件分享
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertFileShare(List<FileShare> fileShareList)
    {
        //获取当前人
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return new AjaxResult(HttpStatus.UNAUTHORIZED,"请先登录");
        }
        if (CollectionUtils.isEmpty(fileShareList)){
            return new AjaxResult(HttpStatus.ERROR,"请选择分享人");
        }
        for (FileShare fileShare : fileShareList) {
            fileShare.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
            fileShare.setUpdateBy(loginUser.getUsername());
            fileShare.setCreateBy(loginUser.getUsername());
            fileShare.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
            fileShare.setState(Constants.DATA_VALID_STATE);
            fileShareMapper.insertFileShare(fileShare);
        }
        return AjaxResult.success();
    }

    /**
     * 修改文件分享
     * 
     * @param fileShare 文件分享
     * @return 结果
     */
    @Override
    public int updateFileShare(FileShare fileShare)
    {
        fileShare.setUpdateTime(DateUtils.getNowDate());
        return fileShareMapper.updateFileShare(fileShare);
    }

    /**
     * 批量删除文件分享
     * 
     * @param ids 需要删除的文件分享主键
     * @return 结果
     */
    @Override
    public int deleteFileShareByIds(Long[] ids)
    {
        return fileShareMapper.deleteFileShareByIds(ids);
    }

    /**
     * 删除文件分享信息
     * 
     * @param id 文件分享主键
     * @return 结果
     */
    @Override
    public int deleteFileShareById(Long id)
    {
        return fileShareMapper.deleteFileShareById(id);
    }
}
